<?php
namespace app\model;

use think\Model;

class BaseModel extends Model
{
    /**
     * 链表数组[]
     * table => 表名
     * on => 链接条件
     * @var array
     */
    public array $customLeft;
    /**
     * 添加一条数据
     * @param array $data  条件
     * @param bool $getLastInsID  返回自增主键
     * @return int|string
     */
    public function add(array $data, bool $getLastInsID = false){
        return $this->insert($data, $getLastInsID);
    }
    /**
     * 添加多条数据
     * @param array $data      条件
     * @return int
     */
    public function addAll(array $data): int
    {
        return $this->insertAll($data);
    }
    /**
     * 修改数据
     * @param array|string $condition   条件
     * @param array $data      字段与值
     * @return bool
     */
    public function edit($condition, array $data): bool
    {
        return $this->where($condition)->save($data);
    }

    /**
     * 数据自增
     * @param array|string $condition  条件
     * @param string $field     需要自增的字段
     * @param int|string $num   自增数量
     * @return bool
     */
    public function editInc($condition, string $field, $num = 1): bool
    {
        return $this->where($condition)->inc($field, $num)->update();
    }

    /**
     * 数据自减
     * @param array|string $condition  条件
     * @param string $field     需要自增的字段
     * @param int|string $num   自增数量
     * @return bool
     */
    public function editDec($condition, string $field, $num = 1): bool
    {
        return $this->where($condition)->dec($field, $num)->update();
    }
    /**
     * 删除数据
     * @param array|string $condition    条件
     * @return bool
     */
    public function del($condition): bool
    {
        return $this->where($condition)->delete();
    }

    /**
     * 查询一条数据
     * @param array|string $condition 条件
     * @param string $field 显示指定的字段
     * @param array $order 排序字段
     * @return array|string
     */
    public function getInfo($condition = [], string $field = "*", array $order = [])
    {
        $data = $this->field($field)->where($condition);
        !empty($order) and $data = $data->order($order);
        $data = $data->find();
        !empty($data) and $data = $data->toArray();
        return is_array($data) ? ((!stripos($field, ',') and $field != '*' ) ? $data[$field] : $data) : [];
    }

    /**
     * 获取总条数
     * @param array|string $condition
     * @return int
     */
    public function getCount($condition): int
    {
        return $this->where($condition)->count();
    }

    /**
     * 查询多条数据
     * @param array|string $condition 条件
     * @param string $field 显示指定的字段
     * @param array|string $order 依据字段排序
     * @param array|int $limit 返回的总条数（可从x条开始）
     * @param string $group 分组
     * @return array
     */
    public function getList($condition = [], string $field = "*", $order = '', $limit = 0, string $group = ''): array
    {
        $sql = $this->field($field)->where($condition)->order($order);
        if(!empty($limit)){
            if(is_array($limit)){
                $sql->limit($limit['start'], $limit['limit']);
            }else{
                $sql->limit($limit);
            }
        }
        !empty($group) and $sql->group($group);
        $data = $sql->select();
        !empty($data) and $data = $data->toArray();
        return is_array($data) ? $data : [];
    }

    /**
     *
     * 查询分页多条数据
     * @param array|string $condition 条件
     * @param string $field 显示指定的字段
     * @param array|string $order 依据字段排序
     * @param array|int $limit 返回的总条数（可从x条开始）
     * @param string $group 分组字段
     * @return array
     */
    public function getPageList($condition = [], string $field = "*", $order = '', $limit = 0, string $group = ''): array
    {
        $return = [];
        $sql = $this;
        if(!empty($this->customLeft) and isset($this->customLeft['table']) and !empty($this->customLeft['table']) and isset($this->customLeft['on']) and !empty($this->customLeft['on'])){
            $sql = $sql->alias($this->name)->leftjoin($this->customLeft['table'], $this->customLeft['on']);
        }
        $sql = $sql->field($field)->where($condition)->order($order);
        !empty($group) and $sql->group($group);
        $return['total'] = ($sql->count());
        if (!empty($limit)) {
            if (is_array($limit)) {
                $sql->limit($limit['start'], $limit['limit']);
            } else {
                $sql->limit($limit);
            }
        }
        $data = $sql->select();
        !empty($data) and $data = $data->toArray();
        $return['rows'] = (array)$data;
        return $return;
    }


    /**
     * 链表查询多条数据
     * @param string $tables 表名
     * @param string $on 链接条件
     * @param array|string $condition 查询条件
     * @param string $field 显示指定的字段
     * @param array|string $order 依据字段排序
     * @param array $limit 返回的总条数（可从x条开始）
     * @param string $group 依据字段分组
     * @return array|null
     */
    public function getJGList(string $tables, string $on, $condition, string $field = "*", $order = '', array $limit = ['start' => 0, 'limit' => 10], string $group = ''): ?array
    {
        empty($limit) and $limit = ['start' => 0, 'limit' => 10];
        empty($group) and $group = $this->getPk();
        $data = $this->alias($this->name)->leftjoin($tables, $on)->field($field)->where($condition)->order($order)->limit($limit['start'], $limit['limit'])->group($group)->select();
        !empty($data) and $data = $data->toArray();
        return is_array($data) ? $data : [];
    }

    /**
     * 链表查询多条数据
     * @param string $tables 表名
     * @param string $on 链接条件
     * @param array|string $condition 查询条件
     * @param string $field 显示指定的字段
     * @param array|string $order 依据字段排序
     * @param string $group 依据字段分组
     * @return array|null
     */
    public function getJGInfo(string $tables, string $on, $condition, string $field = "*", $order = '', string $group = ''): ?array
    {
        empty($group) and $group = $this->getPk();
        $data = $this->alias($this->name)->leftjoin($tables, $on)->field($field)->where($condition)->order($order)->group($group)->find();
        !empty($data) and $data = $data->toArray();
        return is_array($data) ? $data : [];
    }

}